package com.yy.myp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yy.myp.R;
import com.yy.myp.entity.MapSearchItem;

import java.util.ArrayList;
import java.util.List;

public class MapSearchItemAdapter extends RecyclerView.Adapter<MapSearchItemAdapter.MapSearchItemAdapterHolder> {

    private List<MapSearchItem> addressList;

    private Context context;

    private OnClickListener onClickListener;

    public MapSearchItemAdapter (Context context) {
        super();
        this.addressList = new ArrayList<>();
        this.context = context;
    }

    public void setAddressList (List<MapSearchItem> addressList) {
        if (addressList == null) {
            return;
        }
        this.addressList = addressList;
        this.notifyDataSetChanged();
    }

    public void addAddress (MapSearchItem address) {
        this.addressList.add(address);
    }

    @NonNull
    @Override
    public MapSearchItemAdapter.MapSearchItemAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MapSearchItemAdapter.MapSearchItemAdapterHolder(LayoutInflater.from(context).inflate(R.layout.mt_map_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MapSearchItemAdapter.MapSearchItemAdapterHolder holder, int position) {
        MapSearchItem searchItem = addressList.get(position);
        holder.addressTitle.setText(searchItem.getKey());
        holder.addressTitle.setOnClickListener(v -> {
            onClickListener.onClick(searchItem);
        });
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onClick(MapSearchItem searchItem);
    }

    class MapSearchItemAdapterHolder extends RecyclerView.ViewHolder {

        private TextView addressTitle;

        public MapSearchItemAdapterHolder(@NonNull View itemView) {
            super(itemView);
            this.addressTitle = itemView.findViewById(R.id.at_map_list_item);
        }
    }

}
